package com.third.device.mg.serialport

import com.third.device.mg.serialport.factory.autorail.AutorailsCRC
import com.third.device.mg.serialport.utils.*
import org.junit.Assert.assertEquals
import org.junit.Test
import kotlin.experimental.and

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }



    @Test
    fun autorailsTest(){
        val msg = ByteArray(5)//0x55 ,0x12 ,0x34 ,0x03 ,0x01
        msg[0] =  0x55.toByte()
        msg[1] =  0x12.toByte()
        msg[2] =  0x34.toByte()
        msg[3] =  0x03.toByte()
        msg[4] =  0x01.toByte()
        val result = AutorailsCRC.crcRTU(msg)

        println("msg. toByteArray(H): ${msg.toHexString()}")
        println("result:$result toByteArray(Int): ${result}")
        println("result:$result toByteArray(H): ${AutorailsCRC.crcRTU2H(msg).toHexString()}")
        println("result:$result toByteArray(L): ${AutorailsCRC.crcRTU2L(msg).toHexString()}")



        val num = 0x010f
        println("num:$num to2ByteL(L)低位在前，高位在后: ${num.to2ByteL().toHexString()}")
        println("num:$num to2ByteH(H)高位在前，低位在后: ${num.to2ByteH().toHexString()}")
        println("num:$num Integer.toHexString(H): ${Integer.toHexString(num)}")


    }

    @Test
    fun to2Str(){
        println(intToBit(0x07))
    }

    /**
     * 将int变成bit数组
     */
    private fun intToBit(b: Int): String {
        return "" + (b shr 7 and 0x1).toByte() +
                (b shr 6 and 0x1).toByte() +
                (b shr 5 and 0x1).toByte() +
                (b shr 4 and 0x1).toByte() + " " +
                (b shr 3 and 0x1).toByte() +
                (b shr 2 and 0x1).toByte() +
                (b shr 1 and 0x1).toByte() +
                (b and 0x1).toByte()
    }

    @Test
    fun testByteArray(){
        val num1 = -1

        println("$num1 Hex: ${num1.to2ByteL().toHexString()}" )
        println("$num1 Hex: ${num1.to2ByteH().toHexString()}" )


        println("0x0000 == >> ${0x0000}" )
        println("0x00FF == >> ${0x00FF}" )
        println("0xFF00 == >> ${0xFF00}" )
        println("0xFFFF == >> ${0x0000}" )
        println("0xFEFE == >> ${0xFEFE}" )
        println("0x96 == >> ${0x29}" )
    }
    @Test
    fun testAdd(){
        val data = byteArrayOf(
            0x5A ,0xA5.toByte() ,0 ,1 ,0 ,1 ,0,0,0,0
        )
        println("data == >> ${data.toHexString()}"  )
        var sum = 0
        data.forEach {
            val num = ((it.toInt() and 0xFF))
            println("num ==> $num")
            sum += num
        }
        println("sum == >> add: $sum    hex:${sum.to2ByteL().toHexString()}"  )

        println("257 == >> ${257.to2ByteL().toHexString()}"  )
    }

    @Test
    fun testToBit(){
        val num = arrayListOf(0 ,1 ,2 ,3 ,4,5,6,65)
        num.forEach {
            println("$it ==> Hex: ${it.toByte().toHexString()}   Bit:${it.toByte().toBitString()} ")
        }

        val bit1= "01000001"
        println("$bit1 == >> ${bit1.bitToByte().toIntU()}")

        val bit2= "000001"
        println("$bit2 == >> ${bit2.bitToByte().toIntU()}")
    }

    @Test
    fun testFS(){
        val num  =  247
        println("$num ==>> ${num.to2ByteH().toHexString()}" )
        short2byteBE(247 ,ByteArray(2) ,0)
    }

    fun short2byteBE(input: Short, output: ByteArray, offset: Int) {
        output[0 + offset] = (input.toInt() shr 8).toByte()
        output[1 + offset] = (input and 0xff).toByte()
        println(output.toHexString())
    }

    @Test
    fun testZero(){
        val num  = 501
        if (num in 1..500){
            println("数字:${num.toZeroString(3)}")
        }else{
            println("数字:不在范围")
        }


    }
    @Test
    fun testByteArrayPlus(){
        val b1 = byteArrayOf(0x01 ,0x02 ,0x03)
        val b2 = byteArrayOf(0x0A ,0x0B ,0x0C)
        println("合并字节数组b1-b2: ${b1.plus(b2).toHexString()}")
        println("合并字节数组b2-b1: ${b2.plus(b1).toHexString()}")
    }
}